import dayjs from 'dayjs'
import { Button } from 'antd'
import { useState } from 'react'
import { useMount } from 'ahooks'

import { WijmoOLAP } from '../../Router/Wijmo'
import { GetIndexedDB } from '../../Utils/IndexedDB'


import { RedoOutlined, SearchOutlined, ExportOutlined, BarChartOutlined, RadarChartOutlined } from '@ant-design/icons';


const wijmoConfig = async () => {
    const options = await GetIndexedDB('options')
    return ({
        get: 'getCaiLiaoYunShuStatistics',
        initCom: 'A01',
        params: {
            statisticstype: 0,
            orgtype: 0,
            pageIndex: 1,
            pageSize: 100000,
        },
        combination: {
            A01: {
                title: '全部费用统计',
                data: {
                    // 总计显示 0:不显示总数 1:显示总数 2:显示小计和总计
                    defaultFilterType: 3,
                    showZeros: false,
                    sortableGroups: true,
                    totalsBeforeData: false,
                    showRowTotals: 2,
                    showColumnTotals: 2,
                    fields: [
                        {
                            binding: 'pguid',
                            header: '组织结构',
                            align: 'center',
                            getValue: (e) => options.orgInfo.find(v => v.uuid === e.pguid).label
                        },
                        { binding: 'shouLiaoYear', header: '收料年份', align: 'center' },
                        { binding: 'shouLiaoMonth', header: '收料月份', align: 'center' },
                        { binding: 'shouLiaoDay', header: '收料日期', align: 'center' },
                        { binding: 'yS_CheHao', header: '车牌号码', align: 'center' },
                        { binding: 'yS_CaiLiaoName', header: '材料名称', align: 'center' },
                        { binding: 'yS_YuanShouLiaoShuLiang1', header: '转换前收料量', align: 'center' },
                        { binding: 'yS_Unit', header: '转换前单位', align: 'center' },
                        { binding: 'ciaoLiaoXiShu', header: '转换系数', align: 'center' },
                        { binding: 'yS_ShouLiaoShuLiang', header: '转换后收料量', align: 'center' },
                        { binding: 'cL_EUnit', header: '转换后单位', align: 'center' },
                        { binding: 'yS_YunShuFang', header: '承运单位', align: 'center' },
                        { binding: 'supplierName', header: '供应商', align: 'center' },
                        { binding: 'yS_FaLiaoYuan', header: '发料员', align: 'center' },
                        { binding: 'yS_ShouLiaoYuan', header: '收料员', align: 'center' },
                        { binding: 'yS_FaLiaoDi', header: '发料地', align: 'center' },
                        { binding: 'yS_ShouLiaoDi', header: '收料地', align: 'center' },
                        { binding: 'yS_HeJiYunJu', header: '运距（公里）', align: 'center' },
                        { binding: 'caiLiaoFeiDanJia', header: '材料费单价', align: 'center' },
                        { binding: 'yS_Price', header: '运费单价', align: 'center' },
                        { binding: 'CaiLiaoFeiJinE', header: '材料费', align: 'center' },
                        { binding: 'yS_Money', header: '运费', align: 'center' },
                        { binding: 'totalAmount', header: '合计金额', align: 'center' },
                        { binding: 'yS_FaLiaoDate', header: '发料时间', align: 'center' },
                        { binding: 'yS_ShouLiaoDate', header: '收料时间', align: 'center' },
                        { binding: 'feiYongType', header: '费用类型', align: 'center' },
                        { binding: 'yS_ShuJuLeiXing', header: '数据来源', align: 'center' },
                    ],
                    filterFields: { items: [] },
                    rowFields: { items: ["组织结构", "材料名称"] },
                    columnFields: { items: ["收料年份", "收料月份"] },
                    valueFields: { items: ["材料费", "运费", "合计金额"] }
                }
            },
            A02: {
                title: '运费统计',
                data: {
                    // 总计显示 0:不显示总数 1:显示总数 2:显示小计和总计
                    defaultFilterType: 3,
                    showZeros: false,
                    sortableGroups: true,
                    totalsBeforeData: false,
                    showRowTotals: 2,
                    showColumnTotals: 2,
                    fields: [
                        {
                            binding: 'pguid',
                            header: '组织结构',
                            align: 'center',
                            getValue: (e) => options.orgInfo.find(v => v.uuid === e.pguid).label
                        },
                        { binding: 'shouLiaoYear', header: '收料年份', align: 'center' },
                        { binding: 'shouLiaoMonth', header: '收料月份', align: 'center' },
                        { binding: 'shouLiaoDay', header: '收料日期', align: 'center' },
                        { binding: 'yS_CheHao', header: '车牌号码', align: 'center' },
                        { binding: 'yS_CaiLiaoName', header: '材料名称', align: 'center' },
                        { binding: 'yS_YuanShouLiaoShuLiang1', header: '转换前收料量', align: 'center' },
                        { binding: 'yS_Unit', header: '转换前单位', align: 'center' },
                        { binding: 'ciaoLiaoXiShu', header: '转换系数', align: 'center' },
                        { binding: 'yS_ShouLiaoShuLiang', header: '转换后收料量', align: 'center' },
                        { binding: 'cL_EUnit', header: '转换后单位', align: 'center' },
                        { binding: 'yS_YunShuFang', header: '承运单位', align: 'center' },
                        { binding: 'supplierName', header: '供应商', align: 'center' },
                        { binding: 'yS_FaLiaoYuan', header: '发料员', align: 'center' },
                        { binding: 'yS_ShouLiaoYuan', header: '收料员', align: 'center' },
                        { binding: 'yS_FaLiaoDi', header: '发料地', align: 'center' },
                        { binding: 'yS_ShouLiaoDi', header: '收料地', align: 'center' },
                        { binding: 'yS_HeJiYunJu', header: '运距（公里）', align: 'center' },
                        { binding: 'caiLiaoFeiDanJia', header: '材料费单价', align: 'center' },
                        { binding: 'yS_Price', header: '运费单价', align: 'center' },
                        { binding: 'CaiLiaoFeiJinE', header: '材料费', align: 'center' },
                        { binding: 'yS_Money', header: '运费', align: 'center' },
                        { binding: 'totalAmount', header: '合计金额', align: 'center' },
                        { binding: 'yS_FaLiaoDate', header: '发料时间', align: 'center' },
                        { binding: 'yS_ShouLiaoDate', header: '收料时间', align: 'center' },
                        { binding: 'feiYongType', header: '费用类型', align: 'center' },
                        { binding: 'yS_ShuJuLeiXing', header: '数据来源', align: 'center' },
                    ],
                    filterFields: { items: [] },
                    rowFields: { items: ["组织结构", "材料名称"] },
                    columnFields: { items: ["收料年份", "收料月份", "承运单位"] },
                    valueFields: { items: ["运距（公里）", "运费"] }
                }
            },
            A03: {
                title: '材料费统计',
                data: {
                    // 总计显示 0:不显示总数 1:显示总数 2:显示小计和总计
                    defaultFilterType: 3,
                    showZeros: false,
                    sortableGroups: true,
                    totalsBeforeData: false,
                    showRowTotals: 2,
                    showColumnTotals: 2,
                    fields: [
                        {
                            binding: 'pguid',
                            header: '组织结构',
                            align: 'center',
                            getValue: (e) => options.orgInfo.find(v => v.uuid === e.pguid).label
                        },
                        { binding: 'shouLiaoYear', header: '收料年份', align: 'center' },
                        { binding: 'shouLiaoMonth', header: '收料月份', align: 'center' },
                        { binding: 'shouLiaoDay', header: '收料日期', align: 'center' },
                        { binding: 'yS_CheHao', header: '车牌号码', align: 'center' },
                        { binding: 'yS_CaiLiaoName', header: '材料名称', align: 'center' },
                        { binding: 'yS_YuanShouLiaoShuLiang1', header: '转换前收料量', align: 'center' },
                        { binding: 'yS_Unit', header: '转换前单位', align: 'center' },
                        { binding: 'ciaoLiaoXiShu', header: '转换系数', align: 'center' },
                        { binding: 'yS_ShouLiaoShuLiang', header: '转换后收料量', align: 'center' },
                        { binding: 'cL_EUnit', header: '转换后单位', align: 'center' },
                        { binding: 'yS_YunShuFang', header: '承运单位', align: 'center' },
                        { binding: 'supplierName', header: '供应商', align: 'center' },
                        { binding: 'yS_FaLiaoYuan', header: '发料员', align: 'center' },
                        { binding: 'yS_ShouLiaoYuan', header: '收料员', align: 'center' },
                        { binding: 'yS_FaLiaoDi', header: '发料地', align: 'center' },
                        { binding: 'yS_ShouLiaoDi', header: '收料地', align: 'center' },
                        { binding: 'yS_HeJiYunJu', header: '运距（公里）', align: 'center' },
                        { binding: 'caiLiaoFeiDanJia', header: '材料费单价', align: 'center' },
                        { binding: 'yS_Price', header: '运费单价', align: 'center' },
                        { binding: 'CaiLiaoFeiJinE', header: '材料费', align: 'center' },
                        { binding: 'yS_Money', header: '运费', align: 'center' },
                        { binding: 'totalAmount', header: '合计金额', align: 'center' },
                        { binding: 'yS_FaLiaoDate', header: '发料时间', align: 'center' },
                        { binding: 'yS_ShouLiaoDate', header: '收料时间', align: 'center' },
                        { binding: 'feiYongType', header: '费用类型', align: 'center' },
                        { binding: 'yS_ShuJuLeiXing', header: '数据来源', align: 'center' },
                    ],
                    filterFields: { items: [] },
                    rowFields: { items: ["组织结构", "材料名称"] },
                    columnFields: { items: ["收料年份", "收料月份", "供应商"] },
                    valueFields: { items: ["材料费"] }
                }
            },
        },
        configuration: {
            isReadOnly: true,
            allowSorting: 'SingleColumn',
            selectionMode: 5,
            customContextMenu: false,
            showDetailOnDoubleClick: false
        },
        optionsBtn: click => ([
            {
                key: '1',
                type: 'group',
                label: '数据操作',
                children: [
                    { key: '1-2', label: <Button type='link' onClick={() => click('onOLAP')}><RadarChartOutlined style={{ color: '#13c2c2' }} />数据中控</Button> },
                    { key: '3-1', label: <Button type='link' onClick={() => click('onChart')}><BarChartOutlined style={{ color: '#13c2c2' }} />查看图例</Button> },
                ],
            },
            {
                key: '0',
                type: 'group',
                label: '数据查询',
                children: [
                    { key: '0-1', label: <Button type='link' onClick={() => click('onRefresh')}><RedoOutlined style={{ color: '#13c2c2' }} />刷新数据</Button> },
                    { key: '0-2', label: <Button type='link' onClick={() => click('onSearch')}><SearchOutlined style={{ color: '#13c2c2' }} />条件搜索</Button> },
                ]
            },
            {
                key: '2',
                type: 'group',
                label: '导出数据',
                children: [
                    { key: '2-1', label: <Button type='link' onClick={() => click('onSaveAct')}><ExportOutlined style={{ color: '#13c2c2' }} />导出选中数据</Button> },
                    { key: '2-2', label: <Button type='link' onClick={() => click('onSaveAll')}><ExportOutlined style={{ color: '#13c2c2' }} />导出全部数据</Button> },
                ],
            },
        ]),
        gridConfig: {
            initParameter: {
                group: [],
                sort: [],
                groupPanel: true,
                filter: true,
                addSum: true,
                addIndex: true,
                topText: 'Ω',
                btmText: 'Ψ',
            },
            configuration: {
                allowSorting: 'MultiColumn',
                isReadOnly: true,
                selectionMode: 5,
                allowMerging: "Cells"
            },
            columns: [ // 字段设置
                {
                    dataIndex: 'yS_Gid',
                    valueType: 'Hidden'
                },
                {
                    title: '组织机构',
                    dataIndex: 'pguid',
                    valueType: 'Select',
                    valueEnum: options.orgInfo,
                    initialValue: options.orgInfo[0].uuid,
                    fieldNames: { value: "uuid" },
                    hideInTable: true,
                },
                {
                    title: '收料年份',
                    dataIndex: 'shouLiaoYear',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '收料月份',
                    dataIndex: 'shouLiaoMonth',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '收料日期',
                    dataIndex: 'shouLiaoDay',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '车牌号码',
                    dataIndex: 'yS_CheHao',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '材料名称',
                    dataIndex: 'yS_CaiLiaoName',
                    valueType: 'SelectApi',
                    valueJugde: 'pguid',
                    valueApi: 'getCiaoLiaoTypeInfoV2',
                    valueParams: 'statisticsName',
                    fieldNames: { value: "uuid" },
                    width: 240,
                    align: 'center',
                },
                {
                    title: '转换前收料量',
                    dataIndex: 'yS_YuanShouLiaoShuLiang1',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '转换前单位',
                    dataIndex: 'yS_Unit',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '转换系数',
                    dataIndex: 'ciaoLiaoXiShu',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '转换后收料量',
                    dataIndex: 'yS_ShouLiaoShuLiang',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '转换后单位',
                    dataIndex: 'cL_EUnit',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '承运单位',
                    dataIndex: 'yS_YunShuFang',
                    valueType: 'SelectApi',
                    valueJugde: 'pguid',
                    valueApi: 'getCiaoLiaoTypeInfoV2',
                    valueParams: 'yunShuFang',
                    fieldNames: { value: "uuid" },
                    width: 240,
                    align: 'center',
                },
                {
                    title: '供应商',
                    dataIndex: 'supplierName',
                    valueType: 'SelectApi',
                    valueJugde: 'pguid',
                    valueApi: 'getCiaoLiaoTypeInfoV2',
                    valueParams: 'supplierName',
                    fieldNames: { value: "uuid" },
                    width: 240,
                    align: 'center',
                },
                {
                    title: '发料员',
                    dataIndex: 'yS_FaLiaoYuan',
                    valueType: 'SelectApi',
                    valueJugde: 'pguid',
                    valueApi: 'getCiaoLiaoTypeInfoV2',
                    valueParams: 'faLiaoYuan',
                    fieldNames: { value: "uuid" },
                    width: 240,
                    align: 'center',
                },
                {
                    title: '收料员',
                    dataIndex: 'yS_ShouLiaoYuan',
                    valueType: 'SelectApi',
                    valueJugde: 'pguid',
                    valueApi: 'getCiaoLiaoTypeInfoV2',
                    valueParams: 'shouLiaoYuan',
                    fieldNames: { value: "uuid" },
                    width: 240,
                    align: 'center',
                },
                {
                    title: '发料地',
                    dataIndex: 'yS_FaLiaoDi',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '收料地',
                    dataIndex: 'yS_ShouLiaoDi',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '运距（公里）',
                    dataIndex: 'yS_HeJiYunJu',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '材料费单价',
                    dataIndex: 'caiLiaoFeiDanJia',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '运费单价',
                    dataIndex: 'yS_Price',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 3,
                    format: 'n2',
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '材料费',
                    dataIndex: 'CaiLiaoFeiJinE',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '运费',
                    dataIndex: 'yS_Money',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '合计金额',
                    dataIndex: 'totalAmount',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                    aggregate: 1,
                    aggTitle: '总计',
                    smlTitle: '小计',
                },
                {
                    title: '发料时间',
                    dataIndex: 'yS_FaLiaoDate',
                    valueType: 'Date',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '收料时间',
                    dataIndex: 'yS_ShouLiaoDate',
                    valueType: 'Date',
                    width: 240,
                    align: 'center',
                    hideInSearch: true,
                },
                {
                    title: '费用类型',
                    dataIndex: 'feiYongType',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },
                {
                    title: '数据来源',
                    dataIndex: 'yS_ShuJuLeiXing',
                    valueType: 'Input',
                    width: 240,
                    align: 'center',
                },

                {
                    title: '收料开始时间',
                    dataIndex: 'begindate',
                    valueType: 'DatePicker',
                    picker: 'date',
                    initialValue: dayjs().add(-2, 'month').format('YYYY-MM-DD'),
                    hideInTable: true,
                },
                {
                    title: '收料结束时间',
                    dataIndex: 'enddate',
                    valueType: 'DatePicker',
                    picker: 'date',
                    initialValue: dayjs().format('YYYY-MM-DD'),
                    hideInTable: true,
                },
            ],
        }

    })
}


const CaiLiaoYunShuTongJi = () => {
    const [WijmoConfig, setWijmoConfig] = useState()
    useMount(async () => setWijmoConfig(await wijmoConfig()))
    return (
        <div className='CaiLiaoYunShuTongJi'>
            {WijmoConfig && <WijmoOLAP Config={WijmoConfig} />}
        </div>
    )
}

export default CaiLiaoYunShuTongJi;